package com.winit.openapi.wh.outbound.builder;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.winit.oms.spi.outbound.vo.order.OutboundOrderPalletDetailVo;
import com.winit.oms.spi.outbound.vo.order.OutboundOrderWinitServiceVo;
import com.winit.oms.spi.v2.tools.result.ProductRecommendProductResult;
import com.winit.oms.spi.v2.tools.result.ResultFeeDetails;
import com.winit.oms.spi.v2.tools.result.ResultWinitProduct;
import com.winit.oms.spi.vo.v2.order.VasAttributeInfo;
import com.winit.oms.spi.vo.v2.order.VasInfo;
import com.winit.oms.spi.vo.v2.response.FeeDetailResult;
import com.winit.oms.spi.vo.v2.response.QueryUsableWinitProductResult;

/**
 * 产品服务构建
 * 
 * @version <pre>
 * Author     Version        Date        Changes
 * dinghua.an      1.0          2016年7月1日    Created
 *
 * </pre>
 * @since 1.
 */
public class OutboundOrderWinitServiceBuilder {

    public static OutboundOrderWinitServiceVo build(JSONObject json) {
        OutboundOrderWinitServiceVo vo = new OutboundOrderWinitServiceVo();

        return vo;
    }

    /**
     * 构建产品费用列表
     * 
     * @param list
     * @return
     */
    public static Map<String, Object> buildWinitProductList(List<QueryUsableWinitProductResult> list) {
        Map<String, Object> result = new HashMap<String, Object>();
        if (null == list || list.size() == 0) {
            return result;
        }
        List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
        for (QueryUsableWinitProductResult item : list) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("currency", item.getCurrency());
            map.put("errorCode", item.getErrorCode());
            map.put("errorMsg", item.getErrorMsg());
            map.put("outboundType", item.getOutboundType());
            map.put("isUploadProductTicket", item.getIsUploadProductTicket());
            map.put("sla", item.getSla());
            map.put("totalAmount", item.getTotalAmount());
            map.put("length", item.getLength());
            map.put("width", item.getWidth());
            map.put("height", item.getHeight());
            map.put("weight", item.getWeight());
            map.put("volume", item.getVolume());
            map.put("trayCount", item.getTrayQty());
            map.put("winitProductCode", item.getWinitProductCode());
            map.put("winitProductName", item.getWinitProductName());
            map.put("EGDVldDescCode", item.getEGDVldDescCode());
            map.put("EGDVldDesc", item.getEGDVldDesc());
            map.put("EGDValidateCode", item.getEGDValidateCode());
            map.put("deliveryID", item.getDeliveryMode());// 对接AD的派送ID
            map.put("estimateCompleteDate", item.getEstimateCompleteDate());
            // 构建费用明细
            List<Map<String, Object>> feeList = new ArrayList<Map<String, Object>>();
            if (null != item.getFeeDetail() && item.getFeeDetail().size() > 0) {
                for (FeeDetailResult feeItem : item.getFeeDetail()) {
                    Map<String, Object> feeMap = new HashMap<String, Object>();
                    feeMap.put("amount", feeItem.getAmount());
                    feeMap.put("chargeCode", feeItem.getChargeCode());
                    feeMap.put("chargeName", feeItem.getChargeName());
                    feeMap.put("currency", feeItem.getCurrency());
                    feeMap.put("serviceCode", feeItem.getServiceCode());
                    feeMap.put("serviceName", feeItem.getServiceName());
                    feeList.add(feeMap);
                }
            }
            map.put("feeDetail", feeList);
            // 构建托盘明细
            List<Map<String, Object>> palletDetailList = new ArrayList<Map<String, Object>>();
            if (null != item.getPalletDetailList() && item.getPalletDetailList().size() > 0) {
                for (OutboundOrderPalletDetailVo palletDetailVo : item.getPalletDetailList()) {
                    Map<String, Object> palletDetailMap = new HashMap<String, Object>();
                    palletDetailMap.put("palletQty", palletDetailVo.getPalletQty());
                    palletDetailMap.put("palletType", palletDetailVo.getPalletType());
                    palletDetailMap.put("length", palletDetailVo.getPlLength());
                    palletDetailMap.put("width", palletDetailVo.getPlWidth());
                    palletDetailMap.put("height", palletDetailVo.getPlHeight());
                    palletDetailList.add(palletDetailMap);
                }
            }
            map.put("palletDetail", palletDetailList);
            // 增值服务明细
            List<VasInfo> valueAddedServiceList = item.getValueAddedServiceList();
            if (null != valueAddedServiceList && valueAddedServiceList.size() > 0) {
                List<Map<String, Object>> valueAddedServiceMapList = new ArrayList<Map<String, Object>>();
                for (VasInfo vo : valueAddedServiceList) {
                    Map<String, Object> valueAddedServiceMap = new HashMap<String, Object>();
                    valueAddedServiceMap.put("serviceCode", vo.getServiceCode());
                    valueAddedServiceMap.put("serviceName", vo.getServiceName());
                    valueAddedServiceMap.put("serviceDesc", vo.getServiceDesc());
                    List<VasAttributeInfo> attributeInfoList = vo.getAttributeInfoList();
                    if (null != attributeInfoList && attributeInfoList.size() > 0) {
                        List<Map<String, Object>> attributeInfoMapList = new ArrayList<Map<String, Object>>();
                        for (VasAttributeInfo attributeInfo : attributeInfoList) {
                            Map<String, Object> attributeInfoMap = new HashMap<String, Object>();
                            attributeInfoMap.put("attrCode", attributeInfo.getAttributeKey());
                            attributeInfoMap.put("attrName", attributeInfo.getAttributeName());
                            attributeInfoMap.put("attrUnit", attributeInfo.getUnit());
                            attributeInfoMapList.add(attributeInfoMap);
                        }
                        valueAddedServiceMap.put("attrList", attributeInfoMapList);
                    }
                    valueAddedServiceMapList.add(valueAddedServiceMap);
                }
                map.put("VASList", valueAddedServiceMapList);
            }
            resultList.add(map);
        }
        result.put("winitProductList", resultList);
        return result;
    }

    public static List<Map<String, Object>> buildCalcDeliveryFeeFeeList(ProductRecommendProductResult productResult,
                                                                        String productCode) {
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        if (null == productResult) {
            return result;
        }
        if (StringUtils.isNotBlank(productCode)) {
            buildCurrentProdcut(productResult.getCurrentProduct(), result);
        } else {
            List<QueryUsableWinitProductResult> queryUsableWinitProductResults = productResult.getResultList();
            if (null == queryUsableWinitProductResults || queryUsableWinitProductResults.size() < 1) {
                return result;
            }
            for (QueryUsableWinitProductResult item : queryUsableWinitProductResults) {
                if (!"0".equals(item.getErrorCode())) {
                    continue;
                }
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("deliveryWayCode", item.getWinitProductCode());
                map.put("ISOCode", item.getCurrency());
                map.put("deliveryWay", item.getWinitProductName());
                map.put("totalFeeUSD", item.getTotalAmount());
                map.put("logisticsTime", item.getSla());
                map.put("outboundMethod", item.getOutboundType());
                map.put("palletNum", item.getTrayQty());
                // 构建费用明细
                List<Map<String, Object>> feeList = new ArrayList<Map<String, Object>>();
                if (null != item.getFeeDetail() && item.getFeeDetail().size() > 0) {
                    for (FeeDetailResult feeItem : item.getFeeDetail()) {
                        Map<String, Object> feeMap = new HashMap<String, Object>();
                        feeMap.put("feeCode", feeItem.getChargeCode());
                        feeMap.put("feeName", feeItem.getChargeName());
                        feeMap.put("feeNumber", feeItem.getAmount());
                        feeMap.put("ISOCode", feeItem.getCurrency());
                        feeList.add(feeMap);
                    }
                }
                map.put("FeeList", feeList);
                // 构建包裹明细(这里有坑)
                Map<String, Object> packInfo = new HashMap<String, Object>();
                if ("PTL".equals(item.getOutboundType())) {
                    // 托盘打包
                    List<OutboundOrderPalletDetailVo> detailVos = item.getPalletDetailList();
                    for (OutboundOrderPalletDetailVo detailVo : detailVos) {
                        packInfo.put("length",
                            packInfo.get("length") == null ? detailVo.getPlLength() : ((BigDecimal) packInfo.get("length")).add(detailVo.getPlLength())
                                .setScale(2, BigDecimal.ROUND_HALF_UP));
                        packInfo.put("width",
                            packInfo.get("width") == null ? detailVo.getPlWidth() : ((BigDecimal) packInfo.get("width")).add(detailVo.getPlWidth())
                                .setScale(2, BigDecimal.ROUND_HALF_UP));
                        packInfo.put("height",
                            packInfo.get("height") == null ? detailVo.getPlHeight() : ((BigDecimal) packInfo.get("height")).add(detailVo.getPlHeight())
                                .setScale(2, BigDecimal.ROUND_HALF_UP));
                    }
                } else {
                    packInfo.put("length", item.getLength());
                    packInfo.put("width", item.getWidth());
                    packInfo.put("height", item.getHeight());
                }
                packInfo.put("weight", item.getWeight());
                packInfo.put("CBM", item.getVolume());
                map.put("packageInfo", packInfo);
                result.add(map);
            }
        }
        return result;
    }
    
    /**
     * 尾程派送费用计算器出参封装
     * @param productResult
     * @param productCode
     * @return
     */
    public static Map<String, Object> buildCalWHDeliveryFeeFeeList(ProductRecommendProductResult productResult,
                                                                        String productCode) {
        if (null == productResult) {
            return null;
        }
        List<QueryUsableWinitProductResult> queryUsableWinitProductResults = productResult.getResultList();
        if (null == queryUsableWinitProductResults || queryUsableWinitProductResults.size() < 1) {
            return null;
        }
        Map<String, Object> result=new HashMap<String, Object>();
        List<Map<String, Object>> unusableProductList = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> usableProductList = new ArrayList<Map<String, Object>>();
        for (QueryUsableWinitProductResult item : queryUsableWinitProductResults) {
            if (!"0".equals(item.getErrorCode())) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("winitProductCode", item.getWinitProductCode());
                map.put("winitProductName", item.getWinitProductName());
                map.put("errorCode", item.getErrorCode());
                map.put("errorMsg", item.getErrorMsg());
                unusableProductList.add(map);
            }else{
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("currency", item.getCurrency());
                map.put("outboundType", item.getOutboundType());
                map.put("isUploadProductTicket", item.getIsUploadProductTicket());
                map.put("sla", item.getSla());
                map.put("totalAmount", item.getTotalAmount());
                map.put("length", item.getLength());
                map.put("width", item.getWidth());
                map.put("height", item.getHeight());
                map.put("weight", item.getWeight());
                map.put("volume", item.getVolume());
                map.put("trayCount", item.getTrayQty());
                map.put("winitProductCode", item.getWinitProductCode());
                map.put("winitProductName", item.getWinitProductName());
                map.put("EGDVldDescCode", item.getEGDVldDescCode());
                map.put("EGDVldDesc", item.getEGDVldDesc());
                map.put("EGDValidateCode", item.getEGDValidateCode());
                map.put("deliveryID", item.getDeliveryMode());// 对接AD的派送ID
                map.put("estimateCompleteDate", item.getEstimateCompleteDate());
                // 构建费用明细
                List<Map<String, Object>> feeList = new ArrayList<Map<String, Object>>();
                if (null != item.getFeeDetail() && item.getFeeDetail().size() > 0) {
                    for (FeeDetailResult feeItem : item.getFeeDetail()) {
                        Map<String, Object> feeMap = new HashMap<String, Object>();
                        feeMap.put("amount", feeItem.getAmount());
                        feeMap.put("chargeCode", feeItem.getChargeCode());
                        feeMap.put("chargeName", feeItem.getChargeName());
                        feeMap.put("currency", feeItem.getCurrency());
                        feeMap.put("serviceCode", feeItem.getServiceCode());
                        feeMap.put("serviceName", feeItem.getServiceName());
                        feeList.add(feeMap);
                    }
                }
                map.put("feeDetail", feeList);
                // 构建托盘明细
                List<Map<String, Object>> palletDetailList = new ArrayList<Map<String, Object>>();
                if (null != item.getPalletDetailList() && item.getPalletDetailList().size() > 0) {
                    for (OutboundOrderPalletDetailVo palletDetailVo : item.getPalletDetailList()) {
                        Map<String, Object> palletDetailMap = new HashMap<String, Object>();
                        palletDetailMap.put("palletQty", palletDetailVo.getPalletQty());
                        palletDetailMap.put("palletType", palletDetailVo.getPalletType());
                        palletDetailMap.put("length", palletDetailVo.getPlLength());
                        palletDetailMap.put("width", palletDetailVo.getPlWidth());
                        palletDetailMap.put("height", palletDetailVo.getPlHeight());
                        palletDetailList.add(palletDetailMap);
                    }
                }
                map.put("palletDetail", palletDetailList);
                usableProductList.add(map);
            }
        }
        result.put("unusableProductList", unusableProductList);
        result.put("usableProductList", usableProductList);
        return result;
    }

    private static void buildCurrentProdcut(ResultWinitProduct currentProduct, List<Map<String, Object>> result) {
        if (!"Y".equals(currentProduct.getIsUsable())) {

        } else {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("deliveryWayCode", currentProduct.getWinitProductCode());
            map.put("ISOCode", currentProduct.getCurrency());
            map.put("deliveryWay", currentProduct.getWinitProductName());
            map.put("totalFeeUSD", currentProduct.getEstimateFee());
            map.put("logisticsTime", currentProduct.getSLA());
            map.put("outboundMethod", currentProduct.getOutboundType());
            map.put("palletNum", currentProduct.getPalletQty());
            // 构建费用明细
            List<Map<String, Object>> feeList = new ArrayList<Map<String, Object>>();
            if (null != currentProduct.getFeeDetails() && currentProduct.getFeeDetails().size() > 0) {
                for (ResultFeeDetails feeItem : currentProduct.getFeeDetails()) {
                    Map<String, Object> feeMap = new HashMap<String, Object>();
                    feeMap.put("feeCode", feeItem.getFeeCode());
                    feeMap.put("feeName", feeItem.getFeeName());
                    feeMap.put("feeNumber", feeItem.getAmount());
                    feeMap.put("ISOCode", feeItem.getCurrencyCode());
                    feeList.add(feeMap);
                }
            }
            map.put("FeeList", feeList);
            // 构建附加费费用明细
            List<Map<String, Object>> vasFeeList = new ArrayList<Map<String, Object>>();
            if (null != currentProduct.getVasFeeDetails() && currentProduct.getVasFeeDetails().size() > 0) {
                for (ResultFeeDetails detail : currentProduct.getVasFeeDetails()) {
                    Map<String, Object> feeMap = new HashMap<String, Object>();
                    feeMap.put("FeeCode", detail.getFeeCode());
                    feeMap.put("FeeName", detail.getFeeName());
                    feeMap.put("FeeNumber", detail.getAmount());
                    feeMap.put("ISOCode", detail.getCurrencyCode());
                    feeMap.put("serviceCode", detail.getServiceCode());
                    feeMap.put("serviceName", detail.getServiceName());
                    Map<String, Object> attrMap = detail.getAttrMap();
                    if (null != attrMap && !attrMap.isEmpty()) {
                        List<Map<String, Object>> attrList = new ArrayList<Map<String, Object>>();
                        Set<String> keys = attrMap.keySet();
                        for (String key : keys) {
                            Map<String, Object> keyMap = new HashMap<String, Object>();
                            keyMap.put("attrCode", key);
                            keyMap.put("attrValue", attrMap.get(key));
                            attrList.add(keyMap);
                        }
                        feeMap.put("attrList", attrList);
                    }
                    vasFeeList.add(feeMap);
                }
            }
            map.put("VASFeeList", vasFeeList);
            // 构建包裹明细
            Map<String, Object> packInfo = new HashMap<String, Object>();
            packInfo.put("length", currentProduct.getLength());
            packInfo.put("width", currentProduct.getWidth());
            packInfo.put("height", currentProduct.getHeight());
            packInfo.put("weight", currentProduct.getWeight());
            packInfo.put("CBM", currentProduct.getVolume());
            map.put("packageInfo", packInfo);
            result.add(map);
        }
    }
}
